package o;

import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.bumptech.glide.load.HttpException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;
import o.sg.d;

public class ss implements sg<InputStream> {
    @VisibleForTesting
    static final c ॱ = new e();
    private volatile boolean ʼ;
    private InputStream ʽ;
    private final ve ˊ;
    private HttpURLConnection ˋ;
    private final int ˎ;
    private final c ˏ;

    interface c {
        HttpURLConnection ˏ(URL url) throws IOException;
    }

    static class e implements c {
        e() {
        }

        public HttpURLConnection ˏ(URL url) throws IOException {
            return (HttpURLConnection) url.openConnection();
        }
    }

    public ss(ve veVar, int i) {
        this(veVar, i, ॱ);
    }

    @VisibleForTesting
    ss(ve veVar, int i, c cVar) {
        this.ˊ = veVar;
        this.ˎ = i;
        this.ˏ = cVar;
    }

    public void ˏ(@NonNull rl rlVar, @NonNull d<? super InputStream> dVar) {
        long ॱ = aaq.ॱ();
        try {
            dVar.ॱ(ॱ(this.ˊ.ˋ(), 0, null, this.ˊ.ˏ()));
            if (Log.isLoggable("HttpUrlFetcher", 2)) {
                Log.v("HttpUrlFetcher", "Finished http url fetcher fetch in " + aaq.ˎ(ॱ));
            }
        } catch (Throwable e) {
            if (Log.isLoggable("HttpUrlFetcher", 3)) {
                Log.d("HttpUrlFetcher", "Failed to load data for url", e);
            }
            dVar.ˏ(e);
            if (Log.isLoggable("HttpUrlFetcher", 2)) {
                Log.v("HttpUrlFetcher", "Finished http url fetcher fetch in " + aaq.ˎ(ॱ));
            }
        } catch (Throwable th) {
            if (Log.isLoggable("HttpUrlFetcher", 2)) {
                Log.v("HttpUrlFetcher", "Finished http url fetcher fetch in " + aaq.ˎ(ॱ));
            }
        }
    }

    private InputStream ॱ(URL url, int i, URL url2, Map<String, String> map) throws IOException {
        if (i >= 5) {
            throw new HttpException("Too many (> 5) redirects!");
        }
        if (url2 != null) {
            try {
                if (url.toURI().equals(url2.toURI())) {
                    throw new HttpException("In re-direct loop");
                }
            } catch (URISyntaxException e) {
            }
        }
        this.ˋ = this.ˏ.ˏ(url);
        for (Entry entry : map.entrySet()) {
            this.ˋ.addRequestProperty((String) entry.getKey(), (String) entry.getValue());
        }
        this.ˋ.setConnectTimeout(this.ˎ);
        this.ˋ.setReadTimeout(this.ˎ);
        this.ˋ.setUseCaches(false);
        this.ˋ.setDoInput(true);
        this.ˋ.setInstanceFollowRedirects(false);
        this.ˋ.connect();
        this.ʽ = this.ˋ.getInputStream();
        if (this.ʼ) {
            return null;
        }
        int responseCode = this.ˋ.getResponseCode();
        if (ˋ(responseCode)) {
            return ˋ(this.ˋ);
        }
        if (ॱ(responseCode)) {
            Object headerField = this.ˋ.getHeaderField("Location");
            if (TextUtils.isEmpty(headerField)) {
                throw new HttpException("Received empty or null redirect url");
            }
            URL url3 = new URL(url, headerField);
            ˏ();
            return ॱ(url3, i + 1, url, map);
        } else if (responseCode == -1) {
            throw new HttpException(responseCode);
        } else {
            throw new HttpException(this.ˋ.getResponseMessage(), responseCode);
        }
    }

    private static boolean ˋ(int i) {
        return i / 100 == 2;
    }

    private static boolean ॱ(int i) {
        return i / 100 == 3;
    }

    private InputStream ˋ(HttpURLConnection httpURLConnection) throws IOException {
        if (TextUtils.isEmpty(httpURLConnection.getContentEncoding())) {
            this.ʽ = aak.ˊ(httpURLConnection.getInputStream(), (long) httpURLConnection.getContentLength());
        } else {
            if (Log.isLoggable("HttpUrlFetcher", 3)) {
                Log.d("HttpUrlFetcher", "Got non empty content encoding: " + httpURLConnection.getContentEncoding());
            }
            this.ʽ = httpURLConnection.getInputStream();
        }
        return this.ʽ;
    }

    public void ˏ() {
        if (this.ʽ != null) {
            try {
                this.ʽ.close();
            } catch (IOException e) {
            }
        }
        if (this.ˋ != null) {
            this.ˋ.disconnect();
        }
        this.ˋ = null;
    }

    public void ॱ() {
        this.ʼ = true;
    }

    @NonNull
    public Class<InputStream> ˋ() {
        return InputStream.class;
    }

    @NonNull
    public rz ˎ() {
        return rz.REMOTE;
    }
}
